﻿using Simple.ISysRepository;
using Simple.Tools;
using Simple.Tools.SnowFlake;
using Simple.Tools.Verify;
using SimpleSaas.Common.DataBase;
using SimpleSaas.Common.Execption;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysRepository
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class SysDepartmentRepository : WholeRepository<SysDepartmentEntity, long>, ISysDepartmentRepository
	{
		[Autowired]
		protected ISimpleMapper simpleMapper { get; set; }

		[Autowired]

		protected ISysTenantRepository tenantRepository { get; set; }

		public SysDepartmentRepository() { }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sysDepartmentDto"></param>
		/// <returns></returns>
		public async Task<long> CreateAsync(SysDepartmentDto sysDepartmentDto)
		{
			var verify = sysDepartmentDto.Verify();
			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}
			if (await tenantRepository.AnyAsync(t=>t.Id==sysDepartmentDto.TenantId.ToInt64()))
			{
				throw new BusinessException("请选择企业!");
			}
			SysDepartmentEntity sysDepartmentEntity = new SysDepartmentEntity();
			sysDepartmentEntity.Id = Worker.GetId();
			simpleMapper.Map(sysDepartmentDto,sysDepartmentEntity);
		
			await this.CreateAsync(sysDepartmentEntity);

			return sysDepartmentEntity.Id;
		}
	}
}
